<template>
  <div class="article-container">
    <!-- 导航栏 -->
    <van-nav-bar
      fixed
      left-arrow
      @click-left="$router.back()"
      title="'文章详情' + article.title"
    ></van-nav-bar>
    <!-- /导航栏 -->

    <!-- 加载中 loading -->
    <van-loading v-show="loading" class="article-loading" />
    <!-- /加载中 loading -->
    <div class="error" v-if="is404">
      <p>文章被外星人吃掉了</p>
      <van-button @click="$router.back()">后退</van-button>
      <van-button @click="$router.push('/')">回主页</van-button>
    </div>
    <!-- 文章详情 -->
    <div class="detail" v-else>
      <h3 class="title">{{ article.title }}</h3>
      <div class="author">
        <van-image
          round
          width="1rem"
          height="1rem"
          fit="fill"
          :src="article.aut_photo"
        />
        <div class="text">
          <p class="name">{{ article.aut_name }}</p>
          <p class="time">{{ article.pubdate | relativeTime }}</p>
        </div>
        <van-button round size="small" type="info" @click="toggleFollow">
          {{ article.is_followed ? "取关" : "+ 关注" }}
        </van-button>
      </div>
      <div class="content">
        <div v-html="article.content">正文</div>
      </div>
      <van-divider>END</van-divider>
      <div class="zan">
        <van-button
          round
          size="small"
          hairline
          type="primary"
          plain
          :icon="article.attitude === 1 ? 'good-job' : 'good-job-o'"
          @click="toggleLike"
          >{{ article.attitude == 1 ? "取消点赞" : "点赞" }}</van-button
        >

        <!-- <van-button
          round
          size="small"
          hairline
          type="danger"
          plain
          icon="delete"
          @click="remove"
          >不喜欢</van-button
        > -->
      </div>
    </div>
    <!-- /文章详情 -->

    <!-- 文章评论 -->
    <Comment />
  </div>
</template>

<script>
import Comment from './comment.vue'
import { getDetail, deleteLike, addLike } from '@/api/article.js'
import { follow, unfollow } from '@/api/user.js'
export default {
  name: 'ArticleIndex',
  components: {
    Comment
  },
  data () {
    return {
      loading: true, // 控制加载中的 loading 状态
      article: {},
      is404: false

    }
  },

  created () {
    this.loadDetail()
  },

  methods: {
    async toggleLike () {
      try {
        const attitude = this.article.attitude
        console.log(this.article)
        const artId = this.article.art_id
        if (attitude === 1) {
          const res = await deleteLike(artId)
          this.article.attitude = -1
          console.log(res)
        } else if (attitude === -1) {
          const res = await addLike(artId)
          console.log(res)
          this.article.attitude = 1
        } else if (attitude === 0) {
          const res = await addLike(artId)
          console.log(res)
          this.article.attitude = 1
        }
        this.$toast.success('操作成功')
      } catch (err) {
        console.log(err)
        this.$toast.fail('操作失败')
      }
    },
    async toggleFollow () {
      try {
        // 当前状态
        const isFollowed = this.article.is_followed
        const userId = this.article.aut_id
        console.log(isFollowed)
        if (isFollowed) {
          // 取关
          const res = await unfollow(userId)
          console.log(res)
        } else {
          const res = await follow(userId)
          console.log(res)
        }
        this.article.is_followed = !isFollowed
        this.$toast.success('操作成功')
      } catch (err) {
        console.log(err)
        this.$toast.fail('操作失败')
      }
    },
    async loadDetail () {
      try {
        this.is404 = false
        this.loading = true
        const res = await getDetail(this.$route.params.id)
        console.log(res)
        this.article = res.data.data
        this.loading = false
      } catch (err) {
        this.loading = false
        console.log(err)
        if (err.response.status === 404) {
          this.is404 = true
        }
      }
    }
    // 不喜欢
    //   async remove () {
    //     const attitude = this.article.attitude
    //     const artId = this.article.art_id
    //     const res = removeLike(artId)
    //     console.log(res)
    //     if (attitude === 1 || attitude === -1) {
    //       const delId = (this.article.attitude = 0)

    //       this.$bus.$emit('remove', delId)
    //       console.log(delId)
    //     }
    //   }
  }
}
</script>

<style scoped lang='less'>
.article-container {
  position: absolute;
  left: 0;
  top: 0;
  overflow-y: scroll;
  width: 100%;
  height: 100%;
}
.article-loading {
  padding-top: 100px;
  text-align: center;
}
.error {
  padding-top: 100px;
  text-align: center;
}
.detail {
  padding: 50px 10px;
  .title {
    font-size: 16px;
  }
  .zan {
    text-align: center;
  }
  .author {
    padding: 10px 0;
    display: flex;
    .text {
      flex: 1;
      padding-left: 10px;
      line-height: 1.3;
      .name {
        font-size: 14px;
        margin: 0;
      }
      .time {
        margin: 0;
        font-size: 12px;
        color: #999;
      }
    }
  }
  .content {
    font-size: 14px;
    overflow: hidden;
    white-space: pre-wrap;
    word-break: break-all;
    /deep/ img {
      max-width: 100%;
      background: #f9f9f9;
    }
  }
}
</style>
